package com.ruoyi.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.HistoricActivity;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.IProcessService;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 审核列Controller
 *
 * @author wolfcode
 * @date 2021-07-20
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController {

    private String prefix = "business/carPackageAudit";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IProcessService processService;
    @Autowired
    private IServiceItemService serviceItemService;

    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping()
    public String carMaintenanceInfo() {
        return prefix + "/carPackageAudit";
    }

    /**
     * 查询审核列列表
     */
    @RequiresPermissions("business:carPackageAudit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit) {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出审核列列表
     */
    @RequiresPermissions("business:carPackageAudit:export")
    @Log(title = "审核列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarPackageAudit carPackageAudit) {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        return util.exportExcel(list, "审核列数据");
    }

    /**
     * 新增审核列
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存审核列
     */
    @RequiresPermissions("business:carPackageAudit:add")
    @Log(title = "审核列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit) {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改审核列
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        mmap.put("carPackageAudit", carPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核列
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @Log(title = "审核列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarPackageAudit carPackageAudit) {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除审核列
     */
    @RequiresPermissions("business:carPackageAudit:remove")
    @Log(title = "审核列", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }

    //查看流程图
    @GetMapping("/processImg/{id}")
    public void processImg(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
        CarPackageAudit audit = carPackageAuditService.getCarPackageAudit(id);
        InputStream inputStream = null;
        if (audit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS) || audit.getStatus().equals(CarPackageAudit.STATUS_REJECT)) {
            List<String> activeActivityIds = processService.getActiveActivityIds(audit.getInstanceId());
            inputStream = processService
                    .getProcessImage(audit.getProcessDefinitionId(), activeActivityIds, Collections.EMPTY_LIST);
        } else {
            inputStream = processService
                    .getProcessImage(audit.getProcessDefinitionId(), Collections.EMPTY_LIST, Collections.EMPTY_LIST);
        }
        IOUtils.copy(inputStream, response.getOutputStream());
    }

    //撤销
    @PostMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(String instanceId) {
        carPackageAuditService.cancelApply(instanceId);
        return success();
    }

    //我的待办
    @GetMapping("/todoPage")
    public String todoPage() {
        return prefix + "/todoPage";
    }

    @PostMapping("/todoList")
    @ResponseBody
    public TableDataInfo todoList(CarPackageAudit carPackageAudit) {
        //startPage();
        List<CarPackageAudit> list = carPackageAuditService.findTodoList(carPackageAudit);
        return getDataTable(list);
    }

    //审批
    @GetMapping("/showVerifyDialog/{taskId}")
    public String showVerifyDialog(@PathVariable("taskId") String taskId, ModelMap mmap) {
        mmap.put("taskId", taskId);
        return prefix + "/taskVerify";
    }

    //保存审批表
    @PostMapping("/complete/{taskId}")
    @ResponseBody
    public AjaxResult complete(@PathVariable("taskId") String taskId, Boolean auditStatus, String comment) {
        carPackageAuditService.complete(taskId, auditStatus, comment);
        return AjaxResult.success();
    }


    //修改表单
    @GetMapping("/openServiceItemsEditPage/{carPackageAuditId}")
    public String edit(@PathVariable("carPackageAuditId") String carPackageAuditId, ModelMap mmap) {
        CarPackageAudit audit = carPackageAuditService.getCarPackageAudit(carPackageAuditId);
        ServiceItem serviceItem = JSON.parseObject(audit.getServiceItemInfo(), ServiceItem.class);
        mmap.put("serviceItem", serviceItem);
        mmap.put("carPackageAuditId", carPackageAuditId);
        return prefix + "/serviceItemEdit";
    }

    //保存表单
    @PostMapping("/serviceItemUpdate")
    @ResponseBody
    public AjaxResult serviceItemUpdate(String carPackageAuditId, ServiceItem serviceItem) {
        carPackageAuditService.updateServiceItem(carPackageAuditId, serviceItem);
        return success();
    }

    //重新申请
    @PostMapping("/reApply")
    @ResponseBody
    public AjaxResult reApply(String taskId, String carPackageAuditId) {
        carPackageAuditService.reApply(taskId, carPackageAuditId);
        return success();
    }

    //我的已办
    @GetMapping("/donePage")
    public String donePage() {
        return prefix + "/donePage";
    }

    @PostMapping("/doneList")
    @ResponseBody
    public TableDataInfo doneList(CarPackageAudit carPackageAudit) {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.findDoneList(carPackageAudit);
        return getDataTable(list);
    }

    //审批历史
    @GetMapping("/historyList/{instanceId}")
    public String historyList(@PathVariable("instanceId") String instanceId, ModelMap mmap) {
        mmap.put("instanceId", instanceId);
        return prefix + "/historyList";
    }

    @PostMapping("/listHistory")
    @ResponseBody
    public TableDataInfo listHistory(String instanceId) {
        startPage();
        List<HistoricActivity> list = carPackageAuditService.selectHistoryList(instanceId);
        return getDataTable(list);
    }
}
